package com.template.app.web.form;

import java.util.ArrayList;
import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.template.app.exception.ObjectNotFoundException;
import com.template.app.model.Template;
import com.template.app.model.TemplateRef;
import com.template.app.service.TemplateService;
import com.template.app.support.ControllerHelper;

//@Controller
//@RequestMapping(Template.NAME)
public class TemplateForm {/*
	
	public static final String VIEW_LIST = Template.NAME + ControllerSupport.VIEW_LIST;
	public static final String VIEW_FORM = Template.NAME + ControllerSupport.VIEW_FORM;

	@Autowired
	private TemplateService service;
	
	// TODO listTemplateRefs
	@ModelAttribute("templateRefs")
	public List<TemplateRef> getTemplateRefs() {
		TemplateRef r1 = new TemplateRef();
		r1.setId(1);
		r1.setName("REF 1");

		TemplateRef r2 = new TemplateRef();
		r2.setId(2);
		r2.setName("REF 2");
		
		List<TemplateRef> list = new ArrayList<TemplateRef>();
		list.add(r1);
		list.add(r2);
		
		return list;
	}
	
	@RequestMapping(value = ControllerSupport.NEW, method = RequestMethod.GET)
	public ModelAndView neww() {
		return ControllerSupport.getBaseModelAndView(VIEW_FORM, Template.class);
	}
	
	@RequestMapping(value = ControllerSupport.ADD, method = RequestMethod.POST)
	public String add(@Valid Template t, BindingResult result, ModelMap model) {
		return saveOrUpdate(t, result, model);
	}
	
	@RequestMapping(value = ControllerSupport.UPDATE, method = RequestMethod.PUT)
	public String update(@PathVariable Integer id, @Valid Template t, BindingResult result, ModelMap model) {
		t.setId(id);
		return saveOrUpdate(t, result, model);
	}
	
	@RequestMapping(value = ControllerSupport.EDIT, method = RequestMethod.GET)
	public ModelAndView edit(@PathVariable Integer id) {
		return ControllerSupport.getModelAndView(VIEW_FORM, service.getFetch(id));
	}
	
	@RequestMapping(value = ControllerSupport.EDIT, method = RequestMethod.DELETE)
	public String delete(@PathVariable Integer id) {
		try {
			service.delete(id);
		} catch (ObjectNotFoundException e) {
			//TODO
		}
		return ControllerSupport.redirect("/template/list");
	}
	
	// TODO add read only attribute for disabling interface editing
	@RequestMapping(value = ControllerSupport.GET, method = RequestMethod.GET)
	public ModelAndView get(@PathVariable Integer id) {			
		Template obj = service.get(id);
		return new ModelAndView(VIEW_FORM, Template.NAME, obj);
	}

	private String saveOrUpdate(Template t, BindingResult result, ModelMap model) {
		if (result.hasErrors()) {			
			return ControllerSupport.handleValidationError(VIEW_FORM, result, model);
		} else {
			service.save(t);
		}
		
		System.out.println("locale: " + LocaleContextHolder.getLocale().toString());
		
		return ControllerSupport.redirect("/template/list");
	}
*/}
